package com.ruike.alisurface.ui.mainPage;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.ICCardUtils;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.BinZhouOutDoodsBean;
import com.ruike.alisurface.bean.ShopDetailBean;
import com.ruike.alisurface.http.BinZhouHttp;
import com.ruike.alisurface.ui.payPage.PayShowShopActivity;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_utils.GsonUtils;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 选择购买领取方式
 */
public class SelectTypeActivity extends BaseAty implements ICCardUtils.OnReadCardListener {

    static final String TAG = "SelectTypeActivity";

    @BindView(R.id.selectType_goback_imgv)
    ImageView gobackImgv;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_select_type;
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected void initData() {
        boolean isOpenAdv = ShareUtils.getInstance().getInt(ShareKey.ADV_TYPE, Constant.ADV_TYPE_NONE) != Constant.ADV_TYPE_NONE;
        if (isOpenAdv) {
            L.iTag(TAG, "有广告，启动倒计时");
            startCountdownTimer(120, null, null);
            gobackImgv.setVisibility(View.VISIBLE);
        }
        initTitleBar(!isOpenAdv);
    }

    @Override
    protected void onStart() {
        super.onStart();
        startReadCard();
    }

    @Override
    public void onResultCardNumber(String cardNumber, String errorMsg) {
        if (cardNumber.isEmpty()) {
            showErrorTip(errorMsg);
            return;
        }
        stopReadCard();
        showProgressDialog("正在获取对应商品信息...");
        BinZhouHttp.getSlotByCard(cardNumber, this);
    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {
        super.onSuccess(requestUrl, requestJsonStr);
        if (BinZhouHttp.RECEIVE_URL.equals(requestUrl)) {
            BinZhouOutDoodsBean binZhouOutDoodsBean = GsonUtils.json2Model(requestJsonStr, BinZhouOutDoodsBean.class);
            BinZhouOutDoodsBean.DataBean data = binZhouOutDoodsBean.getData();
            List<BinZhouOutDoodsBean.DataBean.DetailsBean> details = data.getDetails();

            List<ShopDetailBean.OrderDetailsBean> orderDetailsBeans = new ArrayList<>();
            for (BinZhouOutDoodsBean.DataBean.DetailsBean binZhouBean : details) {
                ShopDetailBean.OrderDetailsBean detailsBean = new ShopDetailBean.OrderDetailsBean();
                detailsBean.setSlotIndex(binZhouBean.getSlotIndex());
                detailsBean.setSlotTypeId(binZhouBean.getSlotTypeId());
                detailsBean.setQuantity(binZhouBean.getQuantity());
                orderDetailsBeans.add(detailsBean);
            }

            ShopDetailBean shopDetailBean = new ShopDetailBean(data.getId());
            shopDetailBean.setDetailOrderList(orderDetailsBeans);

            Bundle bundle = new Bundle();
            bundle.putString("pay_type", "binZhouCard");
            bundle.putSerializable("orderBeanData", shopDetailBean);

            startActivity(PayShowShopActivity.class, bundle);
        }
    }

    @Override
    public void onError(String requestUrl, String errorMsg) {
        super.onError(requestUrl, errorMsg);
        if (BinZhouHttp.RECEIVE_URL.equals(requestUrl)) {
            showErrorTip(errorMsg);
            new CountDownTimer(3000, 1500) {

                @Override
                public void onTick(long l) {
                }

                @Override
                public void onFinish() {
                    startReadCard();
                }
            }.start();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopReadCard();
    }

    private void startReadCard() {
        L.iTag(TAG, "开始读卡");
        ICCardUtils.setOnICCardGetPwdListener(this); // 设置读卡监听
        ICCardUtils.readWriteMode(true); // 设置开始读卡
    }

    private void stopReadCard() {
        L.iTag(TAG, "停止读卡");
        ICCardUtils.readWriteMode(false); // 设置停止读卡
        ICCardUtils.setOnICCardGetPwdListener(null); // 设置读卡监听
    }

    @Override
    public void doEventBusResult(MsgEventBus event) {
    }

    public void goback(View view) {
        startCountdownTimer(0, null, null);
    }

    public void jumpMainActivity(View view) {
        boolean isShowSlot = ShareUtils.getInstance().getInt(ShareKey.IS_SHOP_TYPE, 1) == 1;
        L.i(isShowSlot ? "按货道展示界面" : "按商品类型展示界面");
        startActivity(isShowSlot ? MainActivity.class : MainTypeActivity.class, null);
    }

}
